﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthBar : MonoBehaviour 
{


	public Transform player;
	public float duration = 3;
	private float endTime;
	private RectTransform rect;
	public RectTransform redRect;
	public RectTransform greenRect;
	public float width;
	public float height;

	void Awake () 
	{
		endTime = Time.time + duration;
		rect = this.GetComponent<RectTransform>();
		redRect.sizeDelta = new Vector2(width,height);
		greenRect.sizeDelta = new Vector2(width,height);
		rect.rotation = Quaternion.identity;
	}
	
	void Update () 
	{
		if ((Time.time>endTime) || player == null || Camera.main == null)
		{
			Destroy(this.transform.gameObject);
		} else
		{
		rect.position = Camera.main.WorldToScreenPoint(new Vector3(player.position.x,player.position.y + 2F,player.position.z));
		}
	}
	public void SetHealth (float percent)
	{	
		endTime = Time.time + duration;
		greenRect.sizeDelta = new Vector2(width * percent,height);
	}

}
